#define _CRT_SECURE_NO_WARNINGS 1



typedef struct QNode {
    struct QNode* _next;
    int _data;
}QNode;

typedef struct MyCircularQueue {
    QNode* _front;
    QNode* _rear;
    QNode* _rear_prev;
    int _capacity;
    int _size;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
    if (0 == k)
        return NULL;
    QNode* newnode = malloc(sizeof(QNode));
    newnode->_next = NULL;
    newnode->_data = 0;
    MyCircularQueue* CirQueue = malloc(sizeof(MyCircularQueue));
    CirQueue->_front = CirQueue->_rear = newnode;
    CirQueue->_capacity = k;
    CirQueue->_size = 0;
    while (k--)
    {
        QNode* next_node = malloc(sizeof(QNode));
        next_node->_next = NULL;
        next_node->_data = 0;
        newnode->_next = next_node;
        newnode = next_node;
    }
    CirQueue->_rear_prev = newnode;
    newnode->_next = CirQueue->_front;
    return CirQueue;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (obj->_rear->_next == obj->_front)
        return false;
    else
    {
        obj->_rear->_data = value;
        obj->_rear = obj->_rear->_next;
        obj->_rear_prev = obj->_rear_prev->_next;
        obj->_size++;
        return true;
    }
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if (obj->_rear == obj->_front)
        return false;
    else {
        obj->_front = obj->_front->_next;
        obj->_size--;
        return true;
    }
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if (obj->_front == obj->_rear)
        return -1;
    return obj->_front->_data;
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if (obj->_front == obj->_rear)
        return -1;
    return obj->_rear_prev->_data;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return !obj->_size;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    return !(obj->_capacity - obj->_size);
}

void myCircularQueueFree(MyCircularQueue* obj) {
    QNode* ptail = obj->_front;
    QNode* pend = obj->_front;
    while (ptail->_next != pend)
    {
        QNode* tmp = ptail->_next;
        free(ptail);
        ptail = tmp;
    }
    free(ptail);
    free(obj);
    obj = NULL;
}

/**
 * Your MyCircularQueue struct will be instantiated and called as such:
 * MyCircularQueue* obj = myCircularQueueCreate(k);
 * bool param_1 = myCircularQueueEnQueue(obj, value);

 * bool param_2 = myCircularQueueDeQueue(obj);

 * int param_3 = myCircularQueueFront(obj);

 * int param_4 = myCircularQueueRear(obj);

 * bool param_5 = myCircularQueueIsEmpty(obj);

 * bool param_6 = myCircularQueueIsFull(obj);

 * myCircularQueueFree(obj);
*/